<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:cms="http://thymeleafexamples">
<head th:include="include/head"><!--引用CSS文件-->
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">


    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>专业管理</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">首页</a></li>
                            <li class="breadcrumb-item active">目录管理</li>
                        </ol>
                    </div>
                </div>
            </div>
            <!-- /.container-fluid -->
        </section>
        <section class="content">
            <div class="card card-info">
                <div class="card-header">
                    <h3 class="card-title">专业信息管理</h3>
                </div>
                <!-- /.card-header -->
                <!-- form start -->
                <form class="form-horizontal" id="form" method="post" th:action="@{/major/majorSave}">
                    <input type="hidden"  id="majorId" name="majorId" th:value="${major.majorId}" />
<!--                    <input type="hidden" name="pId" th:value="${bean.pid}" />-->
                    <div class="card-body">
                        <div class="form-group row">
                            <label for="majorName" class="col-sm-2 col-form-label">专业名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="majorName"  required
                                       id="majorName" placeholder="专业名称" aria-required="true"
                                       th:value="${major.majorName}">
                                <label class="help-block m-b-none" for="majorName" id="name-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>

<!--                        <div class="form-group row">-->
<!--                            <label for="majorDesc" class="col-sm-2 col-form-label">专业描述</label>-->
<!--                            <div class="col-sm-10">-->
<!--                                <textarea  id="majorDesc"  name="majorDesc" th:text="${major.majorDesc}"  class="form-control" rows="4" cols="4" ></textarea>-->
<!--                                &lt;!&ndash;<label class="help-block m-b-none" for="descritpion" id="descritpion-error"><i class="far fa-bell"></i>必填项</label>&ndash;&gt;-->
<!--                            </div>-->
<!--                        </div>-->
                        <div class="form-group row">
                            <label for="back5" class="col-sm-2 col-form-label">编码</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="back5"   required
                                       id="back5" placeholder="编码 " aria-required="true"
                                       th:value="${major.back5}">
                                <label class="help-block m-b-none" for="back5" id="back5-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="majorType" class="col-sm-2 col-form-label">专业层次</label>
                            <div class="col-sm-10">
                                <select class="form-control" id="majorType" name="majorType" aria-required="true">
                                    <option value="1">请选择</option>
                                    <option value="4" th:selected="${major.majorType}==4">专科</option>
                                    <option value="1" th:selected="${major.majorType}==1">本科</option>
                                    <option value="2" th:selected="${major.majorType}==2">硕士</option>
                                    <option value="3" th:selected="${major.majorType}==3">博士</option>
                                </select>
                                <label class="help-block m-b-none" for="majorType" id="majorType-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>
                    </div>
                    <!-- /.card-body -->
                    <div class="card-footer">
                        <button type="submit" class="btn btn-info">保存</button>
                        <button type="button" class="btn btn-default float-right" th:attr="data-pid=|${major.majorId}" id="back">取消</button>
                    </div>
                    <!-- /.card-footer -->
                </form>
            </div>
        </section>

    </div>
</div>
<!-- ./wrapper -->
<div th:include="include/foot"></div>
<script>
    $.validator.setDefaults({
        highlight : function(a) {
            $(a).removeClass("is-valid").addClass(
                "is-invalid");
        },
        success : function(a) {
            $(a).prev().removeClass("is-invalid").addClass(
                "is-valid");
            $(a).html('<i class="fas fa-check"></i>验证成功');
        },
        errorClass:"help-block m-b-none",
// 		validClass:"help-block m-b-none",
        errorPlacement:function(a,b){
            if(b.is(":radio")||b.is(":checkbox")){
                a.appendTo(b.parent().parent().parent())
            }else{
                a.appendTo(b.parent())
            }},
        submitHandler:function(form){loadPage();form.submit();}
    });

    $.validator.addMethod("checkCode", function(value, element) {
        console.log(value)
        if (value == '') {
            return true;
        }
        let result = false;
        let id = $("#majorId").val();
        $.ajax({
            url : 'checkCode?id=' + id + '&code=' + value+ '&type=' + 1,
            async : false,
            success : function(e) {
                result = e.res;
            },error:function(e){
                layer.msg(e.responseText, {icon: 5});
            }
        });
        return result;
    }, "");
    $.validator.addMethod("checkName", function(value, element) {
        console.log(value)
        if (value == '') {
            return true;
        }
        let result = false;
        let id = $("#majorId").val();
        $.ajax({
            url : 'checkCode?id=' + id + '&code=' + value+ '&type=' + 2,
            async : false,
            success : function(e) {
                result = e.res;
            },error:function(e){
                layer.msg(e.responseText, {icon: 5});
            }
        });
        return result;
    }, "");
    $().ready(function() {
        $("#back").click(function() {
            loadPage();
            //let pId=$(this).data('pid');
            //console.log(pId);
            // location.href = 'majorList?pId='+pId;
            location.href = 'majorList';
        });
        $("#form").validate({
            rules : {
                name : "required",
                descritpion : "required",
                url:"required",
                isShow:{
                    required:true,
                    minlength:1
                },
                majorName : {
                    required:true,
                    checkName : true
                },
                back5 : {
                    required:true,
                    checkCode : true,
                    maxlength:20
                },
            },
            messages : {
                name : '<i class="far fa-times-circle"></i>请输入目录名称',
                descritpion : '<i class="far fa-times-circle"></i>请输入目录描述',
                url :'<i class="far fa-times-circle"></i>请输入目录链接',
                isShow:'<i class="far fa-times-circle"></i>请选择是否显示',
                majorName : {
                    required : '<i class="far fa-times-circle"></i>请输入专业名称',
                    checkName : '<i class="far fa-times-circle"></i>专业已存在'
                },
                back5 : {
                    required : '<i class="far fa-times-circle"></i>请输入编码',
                    checkCode : '<i class="far fa-times-circle"></i>编码已经使用',
                    maxlength : '代码最大长度为{0}'
                },
            }
        });
    });
    loadPage=function(){
        window.parent.loadPage();
    }
</script>
</body>
</html>